<template>
  <Spin :spinning="loading" tip="Loading...">
  <div>
    <!-- 旧件状态修改 - 编辑页面 -->
    <BreakSpace label="旧件状态" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel1" :list="formList" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 3rem;"></div>
    <div
      :style="{
        position: 'fixed',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
  </div>
  </Spin>
</template>
<script>
import { getPartType } from '@/api/dictionary/index.js';
import { editSave,selectById } from '@/api/usedPartsReturnManage/usedPartsReturn_OEM/usedPartsStatusUpdate';
import { dictionary } from '@/utils/dictMixin';
export default {
  props: ['rowData'],
  mixins: [dictionary],
  data() {
    return {
      loading: false,
      dataList: [],
      huanxiajiandataList: [],
      fujiaxiangmudataList: [],
      visible: false,
      editData: null,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中服务站
      formList: this.createFormList(),
      saveData: null
    };
  },
  async created() {
    this.loading = true;
    await this.chuqi();
    await this.chuqi2();
    // this.chuqi3();
    // this.chuqi4();
    await this.init(this.rowData);
    this.loading = false;
  },
  methods: {
    async init(val) {
      let newVar = await selectById({id: val});
      this.editData = newVar.data;
      console.log("=== 通过ID查询结果返回 ==== ",newVar," ======");
      this.formList.forEach(x => {
        console.log(newVar);
        x.initialValue = newVar.data[x.fieldName];
        if (x.fieldName === 'OldPartStatus') {
          x.initialValue = newVar.data.partStatus;
          if (x.initialValue === 55221002) {
            this.formList.find(item => item.fieldName === 'inInfo').rules[0].required = true;
          } else {
            this.formList.find(item => item.fieldName === 'inInfo').rules[0].required = false;
          }
        }
      });

    },
    //旧件状态下拉框
    async chuqi() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5522 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        // this.formList.find(x => x.fieldName == 'partStatus').initialValue = '';
        // this.formList.find(x => x.fieldName == 'partStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
        this.formList.find(x => x.fieldName == 'partStatus').initialValue = '';
        this.formList.find(x => x.fieldName == 'OldPartStatus').initialValue = '';
        this.formList.find(x => x.fieldName == 'partStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
        this.formList.find(x => x.fieldName == 'OldPartStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    //不合格原因下拉框
    async chuqi2() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5590 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'inInfo').initialValue = '';
        this.formList.find(x => x.fieldName == 'inInfo').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    // //索赔类型下拉框
    // async chuqi3() {
    //   console.log("===== 获取下拉框数据 ====== ")
    //   const res = await getPartType({ type: 5501 });
    //   console.log("==========",res)
    //   if (res.resultCode == 200) {
    //     this.formList.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
    //     this.formList.find(x => x.fieldName == 'cmsClaimType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
    //   }
    // },
    // //是否主损件下拉框
    // async chuqi4() {
    //   console.log("===== 获取下拉框数据 ====== ")
    //   const res = await getPartType({ type: 5532 });
    //   console.log("==========",res)
    //   if (res.resultCode == 200) {
    //     this.formList.find(x => x.fieldName == 'mainStatus').initialValue = '';
    //     this.formList.find(x => x.fieldName == 'mainStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
    //   }
    // },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();

      if (this.saveData !== null) {

        let save = {};
        save = this.saveData;

        for (let a in this.editData) {
          if (save[a]) {
            this.editData[a] = save[a];
          }
        }

        if (this.formList.find(x => x.fieldName === 'inInfo').initialValue === '') {
          this.editData.inInfo = undefined;
        }
        this.loading = true;
        let res = await editSave(this.editData);

        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({title: '提示信息', message: '保存成功'});
        } else {
          this.loading = false;
          this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
          // this.closeDialog();
        }

      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    },
    add() {},
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '服务站代码',
          fieldName: 'ownerCode',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '服务站名称',
          fieldName: 'ownerName',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '回运通知单号',
          fieldName: 'returnNo',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '索赔单号',
          fieldName: 'cmsClaimNo',
          readonly: true, // 只读
          disabled: true // 置会
        },
        { type: 'SELECT',
          label: '动力形式',
          fieldName: 'ifComm',
          placeholder: '请选择动力形式...',
          initialValue: '',
          itemList: this.createDictList('5543'),
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '旧件代码',
          fieldName: 'partCode',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '旧件名称',
          fieldName: 'partName',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '是否主损件',
          fieldName: 'isMain',
          itemList: [],
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '主损件号',
          fieldName: 'mainPartCode',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '主损件名称',
          fieldName: 'mainPartName',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          placeholder: '请输入车型...',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '索赔类型',
          fieldName: 'cmsClaimTypeStr',
          itemList: [],
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '存放仓库',
          fieldName: 'whName',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '旧件数量',
          fieldName: 'partCount',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '旧件原状态',
          itemList: [],
          fieldName: 'OldPartStatus',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '物流',
          fieldName: 'expressNo',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'DATE',
          label: '入库时间',
          fieldName: 'inStockDate',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '入库人',
          fieldName: 'inByStr',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '旧件状态',
          itemList: [],
          fieldName: 'partStatus',
          rules: [
            {
              required: true,
              message: '请选择旧件状态',
              trigger: 'change'
            }
          ],
          readonly: true,// 只读
            change: val => {
              if (val === 55221002) {
                this.formList.find(item => item.fieldName === 'inInfo').rules[0].required = true;
              } else {
                this.formList.find(item => item.fieldName === 'inInfo').rules[0].required = false;
                this.formList.find(item => item.fieldName === 'inInfo').initialValue = undefined;
              }
          }
        },
        {
          type: 'SELECT',
          label: '不合格原因',
          itemList: [],
          fieldName: 'inInfo',
          rules: [
            {
              required: false,
              message: '请选择不合格原因',
              trigger: 'change'
            }
          ],
          readonly: true // 只读
        },
        // {
        //   type: 'INPUT',
        //   label: '入库说明',
        //   fieldName: 'inInfo2',
        //   readonly: true // 只读
        // }
      ];
    }

  }
};
</script>
<style lang="less" scoped></style>
